encode array to JSON in PHP to get [elm1,elm2,elm3] instead of {elm1:{},elm2:{},elm3:{}}

Posted by Itay Moav on Stack Overflow See other posts from Stack Overflow or by Itay Moav
Published on 2009-03-26T01:59:41Z Indexed on 2010/06/08 13:52 UTC
Read the original article Hit count: 195

Filed under:
|
|
|

I am trying to json_encode an array, which is what returns from a Zend_DB query. var_dump gives: (Manually adding 0 member does not change the picture).

array(3) {
  [1]=>
  array(3) {
    ["comment_id"]=>
    string(1) "1"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "test 1"
  }
  [2]=>
  array(3) {
    ["comment_id"]=>
    string(1) "2"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "test 1"
  }
  [3]=>
  array(3) {
    ["comment_id"]=>
    string(1) "3"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "jhghjg"
  }
}

The encoded string looks:

{"1":{"comment_id":"1","erasable":"1","comment":"test 1"},
 "2":{"comment_id":"2","erasable":"1","comment":"test 1"},
 "3":{"comment_id":"3","erasable":"1","comment":"jhghjg"}}

While I need it as: [{"comment_id":"1","erasable":"1","comment":"test 1"}, {"comment_id":"2","erasable":"1","comment":"test 1"}, {"comment_id":"3","erasable":"1","comment":"jhghjg"}]

As the php.ini/json__encode documentation says it should. Any ideas?

© Stack Overflow or respective owner

Related posts about php

Related posts about JSON